﻿using System;
using System.Data;
using System.Configuration;

using System.Collections;
using idea.entity;
using System.Text;
/// <summary>
/// crm_user 的摘要说明
/// </summary>
/// 
public class menu_item
{
    private string m_name;
    private string m_address;
    private string m_parent;
 
     public string name
    {
        get
        {
            return m_name;
        }
    }
    public string address
    {
        get
        {
            return m_address;
        }
    }
    public string parent
    {
        get
        {
            return m_parent;
        }
    }

    public menu_item(string p1, string p2, string p3)
    {
        m_name = p1;
        m_address = p2;
        m_parent = p3;
        
    }
}
public class crm_user
{
    private string m_lastUrl;
    public string lastUrl
    {
        get
        {
            return m_lastUrl;
        }
        set
        {
            m_lastUrl = value;
        }
    }

    ArrayList m_menu;
    public ArrayList menu
    {
        get
        {
            return m_menu;
        }
    }
    public enum user_state
    {
        __not_login, __login
    }
    private user_state m_state;
    private users m_user;
    public users user
    {
        get
        {
            return m_user;
        }
    }
 

    public crm_user()
    {
        m_state = user_state.__not_login;
        m_user = new users();
        m_menu = new ArrayList();
        m_lastUrl = "http://www.ex-custeel.com";
    }
    public bool isLogin
    {
        get
        {
            if (m_state == user_state.__login)
                return true;
            else
                return false;
        }
    }
    private void loginCheck(string userName, string userPassword)
    {
        if (userName == "")
            throw (new Exception("用户名不能为空!"));
        if (userPassword == "")
            throw (new Exception("密码不能为空!"));
        if (userName.Length > 20)
            throw (new Exception("用户名太长"));
        if (userPassword.Length > 20)
            throw (new Exception("密码太长!"));
        //if (userPassword.Length < 6)
        //  throw (new Exception("密码太短!")); 
    }
    public void checkOwer(int ownerType, string userid, string groupid)//检查是否可以修改他人记录，即 详操作他人的权限
    {
       
           
    }
    public void checkOwerQuery(int ownerType, string userid, string groupid)//检查是否可以查询他人记录，即 详操作他人的权限
    {

    

    }

    public bool checkQueryAll(string name)
    {
        if (user.userType == users.SUPPER)
            return true;
        else
        {
            try { checkPermission("query-others-record"); }
            catch { return false; }
            return true;
        }
    }
    public string  checkPermission(string url)
    {
        string pName="";
        if (isLogin)
        {
          
            bool hasPermission = false;
            if (user.userType != users.SUPPER)
            {
                foreach (object obj in m_menu)
                {
                    menu_item m = (menu_item)obj;

                    if (url.ToLower() == m.address.ToLower())
                    {
                        hasPermission = true;
                        pName = m.name;
                        break;
                    }
                }
                if (!hasPermission)
                    throw (new Exception("您没有权限！"));
            }
        }
        else
        {
            throw (new Exception("您没有登陆！请先登陆！"));
        }
        return pName;
    }
    public void login(string userName, string userPassword)
    {

        userName = userName.Trim();
        userPassword = userPassword.Trim();

        loginCheck(userName, userPassword);//参数格式验证
        if (m_user == null)
            m_user = new users();
        m_user.initByQuery(string.Format("where userName='{0}'", userName));
        if (m_user.userState == users.USERSTOP)
            throw (new Exception("用户已经被停用！"));
        if (m_user.userPassword != userPassword)
            throw (new Exception("login:密码错误"));
        m_state = user_state.__login;

        initMenu();




    }
    public void checkTimeLimit(DateTime time1)
    {
        if (time1 < DateTime.Today)
        {
            try { checkPermission("modify-history-record"); }
            catch { throw (new Exception("您没有修改历史数据的权限")); }
        }
        else
        {
            
            DateTime timeLimt = DateTime.Today + TimeSpan.FromHours(20);
            DateTime timeNow = DateTime.Now;
            if (timeNow > timeLimt)
            {
                try { checkPermission("modify-today-timeout-record"); }
                catch { throw (new Exception("您没有权限修改当天20点后的数据")); }
                
            }
        }
    }
    public void initMenu()
    {

        if (user.userType != users.SUPPER)
        {
            userActor a = new userActor();
            DataSet set = a.query(string.Format(" where userid='{0}' ", user.userID), "");
            DataTable dt = set.Tables[0];

            StringBuilder sb = new StringBuilder("");
            int i = 0;
            foreach (DataRow row in dt.Rows)
            {
                i++;
                if (i == 1)
                    sb.Append(string.Format("where actorID='{0}' ", row["actorID"].ToString()));
                else
                    sb.Append(string.Format("or actorID='{0}' ", row["actorID"].ToString()));


            }
            if (i == 0)
                return;
            actorPermission ap = new actorPermission();
            set = ap.query(sb.ToString(), "");
            dt = set.Tables[0];

            sb.Remove(0, sb.Length);
            i = 0;
            foreach (DataRow row in dt.Rows)
            {
                i++;
                if (i == 1)
                    sb.Append(string.Format("where permissionID='{0}' ", row["permissionID"].ToString()));
                else
                    sb.Append(string.Format("or permissionID='{0}' ", row["permissionID"].ToString()));

            }
            if (i == 0)
                return;
            permission p = new permission();
            set = p.query(sb.ToString(), "order by sortNO");
            dt = set.Tables[0];
            m_menu.Clear();
            foreach (DataRow row in dt.Rows)
            {
                //  row["permissionName"].ToString(), p.permissionAddress
                m_menu.Add(new menu_item(row["permissionName"].ToString(), row["permissionAddress"].ToString(), row["parent"].ToString()));
            }
        }
        else
        {
            permission p = new permission();
            DataSet set = p.query("", "order by sortNO");
            DataTable dt = set.Tables[0];
            m_menu.Clear();
            foreach (DataRow row in dt.Rows)
            {
                //  row["permissionName"].ToString(), p.permissionAddress
                m_menu.Add(new menu_item(row["permissionName"].ToString(), row["permissionAddress"].ToString(), row["parent"].ToString()));
            }
     

        }
    }
   
}
